/**
 * 
 */
package com.door.erp.sevice.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.door.erp.dao.EmployeeDao;
import com.door.erp.dao.base.GenericDao;
import com.door.erp.model.Employee;
import com.door.erp.pager.Pager;
import com.door.erp.sevice.EmployeeService;
import com.door.erp.sevice.base.GenericServiceImpl;

/**
 * @author xiaolongwei@gmail.com
 *
 * 
 */
@Service("employeeService")
public class EmployeeServiceImpl  extends GenericServiceImpl<Employee, Long> implements
EmployeeService {
	@Resource(name="employeeDao", type=EmployeeDao.class)
    private EmployeeDao employeeDao;
	@Override
	public GenericDao<Employee, Long> getDao() {		
		return employeeDao;
	}
	@Override
	public String generatePwd(){
		char[] numbers = {1,2,3,4,5,6,7,8,9,0};
		Random r = new Random();
		StringBuffer pwd = new StringBuffer();
		for(int i =0; i<6;i++){
			int y = r.nextInt(10);
			if(y==10){
				y--;
			}
			y = numbers[y];
			pwd.append(y);
		}
		return pwd.toString();
	}
	@Override
	public String saveEmployeeImg(MultipartFile img, File dir) {
		if (dir == null || !dir.exists()) {
			dir.mkdirs();
		}
		if(img==null){
			return null;
		}
		if(!img.getContentType().contains("image")){
			return null;
		}
		
		String oldName = img.getOriginalFilename();
		String imgSuffix = oldName.substring(oldName.lastIndexOf(".") + 1);
		String newName = UUID.randomUUID().toString() +"." +imgSuffix;
		File newFile = new File(dir.getAbsolutePath(), newName);
	
		try {
			img.transferTo(newFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return newName;
	}
	@Override
	public List<Employee> getValidateResultSet(Map<String, Object> param,
			Pager pager) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public int getValidateResultSetCount(Map<String, Object> param) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public List<Employee> getValidateResultSet(Map<String, Object> param) {
		// TODO Auto-generated method stub
		return null;
	}
}
